// pages/answer/index.js
var app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    quest_selects: 0,  //选择答案
    selects_box: [],  //答案数组
    topic: '我最喜欢的旅游出行方式是?',
    lis: '1/10',
    ansewrs: [],
  },
  
  reply: function (e) {
    var that = this;
    var selects_box = that.data.selects_box; //答案库
    var quest_box = that.data.quest_box; //题库
    var quest_selects = e.currentTarget.dataset.quest_selects;//当前题目的排序
    var id = e.currentTarget.dataset.id;//当前题目的选项的id
    var index = e.currentTarget.dataset.index;//当前题目的选项的下标
    var correct = e.currentTarget.dataset.correct;
  
    // if (id == correct) {
      quest_box[quest_selects].text[index].state = id;
    // } else {
    //   quest_box[quest_selects].text[index].state = id;
    //   for (var i in quest_box[quest_selects].text) {
    //     if (quest_box[quest_selects].text[i].id == correct) {
    //       quest_box[quest_selects].text[i].state = correct;
    //     }
    //   }
    // }
    
    console.log(quest_box);
    that.setData({
      quest_box: quest_box,
    })
    setTimeout(function () { 
      ++quest_selects;
      if (quest_box[quest_selects] != undefined) {
        that.setData({
          quest_selects:quest_selects,
        })
      }           
     }, 200);
    // for (var i in quest_box[0].text) {
    //   if (quest_box[0].text[i].id == selects_box[0].id) {
    //     that.setData({
    //       selects: i,
    //     })
    //   }
    // }

      if (quest_box[++quest_selects] == undefined){
        console.log('答题结束');
        wx.request({
          url: app.url +'api/answer',
          data: {
            selects_box:that.data.selects_box,
            openid: wx.getStorageSync('openid'),
            cid:that.data.cid,
          },
          header: { 'content-type': 'application/json'},
          method: 'POST',
          success: function(res) {
              console.log(res.data);
              wx.reLaunch({
                url: '../finsh/index?num=' + res.data + '&cid=' + that.data.cid,
              })
          },
        })
      }

      that.setData({
        quest_selects: --quest_selects,
      })
    // if (quest_box[quest_selects] != undefined && quest_box == quest_box.length) {
    //   that.setData({
    //     quest_box: quest_box,
    //   })
    //   wx.navigateTo({
    //     url: '../share/index',
    //   })
    // }
  },


  selets: function (e) {
    var that = this;
    var id = e.currentTarget.dataset.id;
    var index = e.currentTarget.dataset.index;
    var title = e.currentTarget.dataset.title;
    var correct = e.currentTarget.dataset.correct;
    console.log(correct);
    var selects_box = that.data.selects_box;
    if (selects_box[index] == undefined) {
      selects_box.push(id);
    } else {
      selects_box[index] = id;
    }
    that.setData({
      selects_box: selects_box,
    })
  },

  
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // that.log_load();
    if (options.scene){
      var scene = decodeURIComponent(options.scene);
      that.setData({ cid: scene });                      
    } else if (options.id){
      that.setData({ cid: options.id });                
    }
    if (wx.getStorageSync('openid') == '') {
      app.load(that.log_load);
    } else {
      that.log_load();
    }
  },
  log_load:function(e,c){
    // console.log(options.id);
    wx.showLoading({ title: 'loading', mask: true })
    var that = this;
    // that.setData({ cid: options.id });
    var openid = wx.getStorageSync('openid');
    if (openid == '' || openid == null) {
      openid = c;
    }
    wx.request({
      url: app.url + 'api/aloadquestion',
      data: {
        openid: openid,
        cid: that.data.cid,
      },
      header: { 'content-type': 'application/json' },
      success: function (res) {
        console.log(res.data);
        var data = res.data;
        if (data.status && data.status == 1) {
          wx.hideLoading();          
          wx.reLaunch({
            url: '../finsh/index?num=' + data.right_num + '&cid=' + that.data.cid,
          })
        } else if (data.status && data.status == 2){
          wx.hideLoading();
          wx.showToast({
            title: data.msg,
            icon: 'loading',
            duration: 1500,
            success: function () {
              setTimeout(function () {
                //要延时执行的代码
                 wx.reLaunch({
                  url: '../index/index',
                })
              }, 1500) //延迟时间 
            }
          })
        }else{
          that.setData({
            quest_box: res.data,
          })
          wx.hideLoading();
        }        
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})